﻿using Jayden.Dll.Cards.Poker.Enums;
using Jayden.Dll.Core.Persistence;
using Jayden.Dll.Core.Persistence.Attributes;

namespace Jayden.Dll.Cards.Poker
{
	[Table]
	public class TexasHoldemPlayerHandSummary : ObjectBase
	{
		private long m_HandId;
		private string m_PlayerName;
		private uint m_SeatNumber;
		private uint m_Position;
		private ulong m_PlayerStack;
		private long m_PlayerBalance;
		private ulong m_PlayerWon;
		private bool m_IsAllIn;
		private bool m_IsAllInPreFlop;
		private CardList m_PlayerCards;
		private TexasHoldemHandCategory m_PlayerHandCategory;
		private PokerStreetType m_FoldStreet;
		private PokerHighHand m_PlayerFinalHand;

		public TexasHoldemPlayerHandSummary(UnitOfWork uow)
			: base(uow, 0)
		{

		}

		[Field(PrimaryKey = true, AutoIncrement = true)]
		public long TexasHoldemPlayerHandSummaryId { get { return (long)ObjectId[0]; } set { ObjectId[0] = value; } }
		[Field]
		public long HandId { get { return m_HandId; } set { m_HandId = value; } }
		[Field]
		public string PlayerName { get { return m_PlayerName; } set { m_PlayerName = value; } }
		[Field]
		public uint SeatNumber { get { return m_SeatNumber; } set { m_SeatNumber = value; } }
		[Field]
		public uint Position { get { return m_Position; } set { m_Position = value; } }
		[Field]
		public ulong PlayerStack { get { return m_PlayerStack; } set { m_PlayerStack = value; } }
		[Field]
		public long PlayerBalance { get { return m_PlayerBalance; } set { m_PlayerBalance = value; } }
		[Field]
		public ulong PlayerWon { get { return m_PlayerWon; } set { m_PlayerWon = value; } }
		[Field]
		public PokerStreetType FoldStreet { get { return m_FoldStreet; } set { m_FoldStreet = value; } }
		[Field]
		public string PlayerHand
		{
			get { return PlayerCards == null ? null : PlayerCards.ToString(); }
			set
			{
				if (value == null)
					PlayerCards = null;
				else
					PlayerCards = new CardList(Card.ParseArray(value));
			}
		}
		[Field]
		public string PlayerHandCategoryKey
		{
			get { return PlayerHandCategory == null ? null : PlayerHandCategory.ToString(); }
			set
			{
				if (string.IsNullOrWhiteSpace(value))
					PlayerHandCategory = null;
				else
					PlayerHandCategory = TexasHoldemHandCategory.Parse(value);
			}
		}
		[Field]
		public PokerHandType? PokerFinalHandType { get { return PlayerFinalHand == null ? null : (PokerHandType?)PlayerFinalHand.Type; } set { } }
		[Field]
		public string PokerFinalHandRanks { get { return PlayerFinalHand == null ? null : Card.GetRanks(PlayerFinalHand.Ranks); } set { } }
		[Field]
		public string PokerFinalHandKickers { get { return PlayerFinalHand == null ? null : Card.GetRanks(PlayerFinalHand.Kickers); } set { } }
		[Field]
		public bool IsAllIn { get { return m_IsAllIn; } set { m_IsAllIn = value; } }
		[Field]
		public bool IsAllInPreFlop { get { return m_IsAllInPreFlop; } set { m_IsAllInPreFlop = value; } }

		public TexasHoldemHandCategory PlayerHandCategory { get { return m_PlayerHandCategory; } set { m_PlayerHandCategory = value; } }
		public CardList PlayerCards
		{
			get { return m_PlayerCards; }
			set
			{
				m_PlayerCards = value;
				if (value != null && value.Count == 2)
					m_PlayerHandCategory = new TexasHoldemHandCategory(value[0], value[1]);
				else
					m_PlayerHandCategory = null;
			}
		}
		public PokerHighHand PlayerFinalHand
		{
			get
			{
				if (m_PlayerFinalHand == null && PlayerCards != null && PlayerCards.Count > 0 && HandSummary != null)
				{
					m_PlayerFinalHand = PokerHighHand.GetBestHand(PlayerCards, HandSummary.CommunityCards);
				}
				return m_PlayerFinalHand;
			}
			set { m_PlayerFinalHand = value; }
		}
		private TexasHoldemHandSummary m_HandSummary;
		public TexasHoldemHandSummary HandSummary
		{
			get
			{
				if (m_HandSummary == null)
				{
					TexasHoldemHandSummaryDataAccess dataAccess = UnitOfWork.DataAccessManager.GetDataAccess<TexasHoldemHandSummary>() as TexasHoldemHandSummaryDataAccess;
					m_HandSummary = dataAccess.Read(HandId);
				}
				return m_HandSummary;
			}
			set
			{
				m_HandSummary = value;
				m_HandId = value.TexasHoldemHandSummaryId;
			}
		}

		public string PositionType
		{
			get
			{
				if (HandSummary.PlayerCount == 2)
				{
					if (m_Position == 0)
						return "Big Blind";
					if (m_Position == 1)
						return "Button/Small Blind";
				}
				else
				{
					if (m_Position == 0)
						return "Small Blind";
					if (m_Position == 1)
						return "Big Blind";
				}
				if (m_Position == HandSummary.PlayerCount - 1)
					return "Button";
				if (m_Position == 3)
					return "UTG";
				return m_Position.ToString();
			}
		}

	}
}
